package top.ajun.mall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 跨域配置(等同于 application.yml 中的跨域配置)
 *
 * @author AJun
 * @date 2020/11/27
 */
@Configuration
public class MallCorsConfig {

    /*
      gateway:
        globalcors:
          # 跨域配置
          cors-configurations:
            '[/**]':
              allowedHeaders: "*"
              allowedOrigins: "*"
              allowedMethods: "*"
              allowCredentials: true
     */
    @Bean
    public CorsWebFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }

}
